package com.redstars.generate.controller;

import com.redstars.generate.entity.SysGenerateTemplate;
import com.redstars.generate.page.PageResult;
import com.redstars.generate.result.Result;
import com.redstars.generate.service.impl.SysGeneratorServiceImpl;
import com.redstars.generate.vo.SysGenerateTemplateVo;
import lombok.AllArgsConstructor;
import com.redstars.generate.service.SysGenerateTemplateService;
import com.redstars.generate.query.SysGenerateTemplateQuery;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;

/**
* 项目模板配置
*
* @author zhouhx 
* @since 1.0.0 2022-10-14
*/
@RestController
@RequestMapping("gen/template")
@AllArgsConstructor
public class SysGenerateTemplateController {
    private final SysGenerateTemplateService sysGenerateTemplateService;
    private final SysGeneratorServiceImpl generatorService;

    @GetMapping("page")
    public Result<PageResult<SysGenerateTemplateVo>> page(@Valid SysGenerateTemplateQuery query){
        PageResult<SysGenerateTemplateVo> page = sysGenerateTemplateService.page(query);

        return Result.ok(page);
    }

    @GetMapping("{id}")
    public Result<SysGenerateTemplateVo> detail(@PathVariable("id") Long id){
        SysGenerateTemplateVo detail = sysGenerateTemplateService.detail(id);

        return Result.ok(detail);
    }

    @PostMapping
    public Result<String> save(@RequestBody SysGenerateTemplateVo vo){
        sysGenerateTemplateService.save(vo);

        return Result.ok();
    }

    @PutMapping
    public Result<String> update(@RequestBody @Valid SysGenerateTemplateVo vo){
        sysGenerateTemplateService.update(vo);

        return Result.ok();
    }

    @DeleteMapping
    public Result<String> delete(@RequestBody List<Long> idList){
        sysGenerateTemplateService.delete(idList);

        return Result.ok();
    }

    @GetMapping("list")
    public Result<List<SysGenerateTemplateVo>> list(SysGenerateTemplateQuery query){
        List<SysGenerateTemplateVo> res = sysGenerateTemplateService.list(query);

        return Result.ok(res);
    }

    /* *
     *
     * @author zhuohx
     * @description 测试模板内容
     * @parms  [vo]
     * @return void
     * @throws
     * @date 2022/11/7 14:06
     */
    @PostMapping("test")
    public Result<String> test(@RequestBody SysGenerateTemplateVo vo) throws Exception {
        return Result.ok(generatorService.test(vo));
    }

    /* *
     *
     * @author zhuohx
     * @description 复制模板
     * @parms  [versionId 模板版本id, templateIds 模板id集合]
     * @return com.redstars.generate.result.Result<java.lang.String>
     * @throws
     * @date 2022/11/8 17:24
     */
    @PostMapping("copyTemplate/{versionId}")
    public Result<String> copyTemplate(@PathVariable("versionId") Long versionId,@RequestBody List<Long> templateIds){
        sysGenerateTemplateService.copyTemplate(versionId,templateIds);
        return Result.ok();
    }
}